#include <QDialogButtonBox>
#include <controls/messagebox/cximessagebox.h>
#include <private/messagebox/cximessagebox_p.h>
#include <ui_CxiMessageBox.h>
CxiMessageBox::CxiMessageBox(QWidget* parent)
	: QDialog(parent),
	  d_ptr(new CxiMessageBoxPrivate(this))// 创建私有实现类实例
{
	Q_D(CxiMessageBox);
	d->useDefaultWindowFlags = true;// 标记使用默认窗口标志
	d->init();                      // 调用Private的初始化逻辑
}
CxiMessageBox::CxiMessageBox(Icon icon, const QString& title, const QString& text,
							 StandardButtons buttons, QWidget* parent,
							 Qt::WindowFlags flags, bool showAsIPTitle, bool modal)
	: QDialog(parent, flags),
	  d_ptr(new CxiMessageBoxPrivate(this))
{
	Q_D(CxiMessageBox);
	// 将参数传递给Private的成员
	d->icon = icon;
	d->title = title;
	d->text = text;
	d->buttons = buttons;
	d->showAsIPTitle = showAsIPTitle;
	d->model = modal;
	d->useDefaultWindowFlags = false;// 标记使用自定义窗口标志
	d->init();                       // 调用Private的初始化逻辑
}
CxiMessageBox::~CxiMessageBox() = default;

CxiMessageBox::StandardButton CxiMessageBox::information(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons, bool showAsIPTitle)
{
	return showMessageBox(parent, Information, title, text, buttons, showAsIPTitle);
}
CxiMessageBox::StandardButton CxiMessageBox::question(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons, bool showAsIPTitle)
{
	return showMessageBox(parent, Question, title, text, buttons, showAsIPTitle);
}
CxiMessageBox::StandardButton CxiMessageBox::success(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons, bool showAsIPTitle)
{
	return showMessageBox(parent, Success, title, text, buttons, showAsIPTitle);
}
CxiMessageBox::StandardButton CxiMessageBox::warning(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons, bool showAsIPTitle)
{
	return showMessageBox(parent, Warning, title, text, buttons, showAsIPTitle);
}
CxiMessageBox::StandardButton CxiMessageBox::critical(QWidget* parent, const QString& title, const QString& text, StandardButtons buttons, bool showAsIPTitle)
{
	return showMessageBox(parent, Critical, title, text, buttons, showAsIPTitle);
}
CxiMessageBox::StandardButton CxiMessageBox::showMessageBox(QWidget* parent, CxiMessageBox::Icon icon, const QString& title, const QString& text, CxiMessageBox::StandardButtons buttons, bool showAsIPTitle)
{
	CxiMessageBox msgBox(icon, title, text, buttons, parent,
						 Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowCloseButtonHint,
						 showAsIPTitle);
	//设置为模态
	msgBox.exec();
	return msgBox.clickedButton();
}

CxiMessageBox::StandardButton CxiMessageBox::clickedButton()
{
	Q_D(CxiMessageBox);
	return d->clickedButton;
}
void CxiMessageBox::setShowAsIPTitle(bool showAsIPTitle)
{
	Q_D(CxiMessageBox);
	d->showAsIPTitle = showAsIPTitle;
}
bool CxiMessageBox::showAsIPTitle()
{
	Q_D(CxiMessageBox);
	return d->showAsIPTitle;
}
void CxiMessageBox::setText(const QString& text)
{
	Q_D(CxiMessageBox);
	d->text = text;
	d->ui->textLabel->setText(d->text);
}
QString CxiMessageBox::text()
{
	Q_D(CxiMessageBox);
	return d->text;
}
void CxiMessageBox::addButton(StandardButton button)
{
	Q_D(CxiMessageBox);
	d->buttons |= button;
	d->ui->buttonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int(d->buttons)));
}
void CxiMessageBox::setIcon(CxiMessageBox::Icon icon)
{
	Q_D(CxiMessageBox);
	d->icon = icon;
	d->loadIcon(icon);
}

void CxiMessageBox::showEvent(QShowEvent* event)
{
	Q_D(CxiMessageBox);
	// 确保至少有一个OK按钮
	if (d->ui->buttonBox->standardButtons() == QDialogButtonBox::NoButton)
	{
		// 若按钮为空，强制设置为OK按钮
		d->buttons = Ok;
		d->ui->buttonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int((d->buttons))));
	}
	d->setIPTitle();
	d->updateSize();
	QDialog::showEvent(event);// 调用父类方法，确保默认行为
}
